package com.kt.thread.demo;

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Created by tangwy on 2016/3/26.
 */
public class BlockingQueueTest {
    //private static final int SIZE=10;
    private final BlockingQueue<Integer> abq=new ArrayBlockingQueue<Integer>(1);

    public void test(){
        final Product product=new Product();
        Consumer consumer=new Consumer();
        new Thread(product).start();
        new Thread(consumer).start();
    }
    class Product implements Runnable{
        @Override
        public void run() {
            try {
               while (true){
                   int i=new Random().nextInt(100);
                   System.out.println("插入元素:"+i);
                   abq.put(i);
                   Thread.sleep(2000);
               }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    class Consumer implements Runnable{
        @Override
        public void run() {
            try {
                while (true){
                    System.out.println("取出元素:" + abq.take());
                    Thread.sleep(2000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}
